package cn.yh.springbootjwt.service.impl;

import cn.yh.springbootjwt.domain.entity.CollectDo;
import cn.yh.springbootjwt.domain.vo.ProductCategoryVo;
import cn.yh.springbootjwt.domain.vo.ProductDtlVo;
import cn.yh.springbootjwt.mapper.CollectMapper;
import cn.yh.springbootjwt.mapper.ProductMapper;
import cn.yh.springbootjwt.service.IProductService;
import cn.yh.springbootjwt.utils.JWTUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;


@Service
public class ProductServiceImpl implements IProductService {
    @Autowired
    private ProductMapper productMapper;

    @Autowired
    private CollectMapper collectMapper;
    @Override
    public List<ProductCategoryVo> selectProductByCategoryId(Long categoryId) {
        return productMapper.selectProductByCategoryId(categoryId);
    }

    @Override
    public ProductDtlVo findByProductId(Long productId) {
        ProductDtlVo productDtlVo =  productMapper.findByProductId(productId);

        try {
            Long userId = JWTUtils.getUserIdByClaim();
            CollectDo collect = collectMapper.isCollect(productId, userId);
            if (collect == null){
                productDtlVo.setIsCollect(1);
            }else {
                productDtlVo.setIsCollect(0);
            }
        }catch (Exception e){
            System.out.println("该用户未登录");
            productDtlVo.setIsCollect(0);
        }
        System.out.println("productDtlVo = " + productDtlVo);
        return productDtlVo;
    }

    @Override
    public ProductDtlVo findById(Long productId) {
        return productMapper.findByProductId(productId);
    }
}
